var http = require("http");
var server = http.createServer();
server.on("request",(req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
    if(req.url == "/product") {
        var product = [
            {
                name:"苹果",
                price: 111
            },
            {
                name:"草莓",
                price: 222
            },
            {
                name:"香蕉",
                price: 333
            }
        ];
        //响应的内容只能是字符串和二进制的数据(buffer)
        //JSON.stringify([]) => "[]"数组变成字符串，JSON.parse("[]") => []字符串变成数组
        res.end(JSON.stringify(product));
    } else {
        res.end("index");
    }
});
server.listen(4000);